$(function () {


    $.get('/admin/category/list', function (res) {
        const data = res.data;
        const html = template('art_category', { data });
        $('#category').html(html)

    })

    const id = localStorage.getItem('id');
    $.get('/admin/article/search', { id }, function (res) {
        $('#inputTitle').val(res.data.title)
        $('#articleContent').val(res.data.content)
        $('#category').val(res.data.categoryId)
        $('#articleDate').val(res.data.date)
        $('#coverimg').prop('src', res.data.cover)
        localStorage.removeItem('id')

    })

    layui.use('laydate', function () {
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#articleDate' //指定元素
        });
    });
    const formdata = new FormData()
    changeCoverImage = (file) => {
        formdata.append('cover', file[0])
    }

    saveArticle = state => {
        addRequest(state)
    }


    function addRequest(state) {
        const title = $('#inputTitle').val()
        const categoryId = +$('#category').val()
        const date = $('#articleDate').val()
        const content = $('#articleContent').val()
        formdata.append('title', title)
        formdata.append('categoryId', categoryId)
        formdata.append('date', date)
        formdata.append('content', content)
        formdata.append('state', state)
        formdata.append('id', id)
        // 发布文章
        $.ajax({
            url: '/admin/article/edit',
            type: 'post',
            data: formdata,
            contentType: false,
            processData: false,
            success(res) {
                layer.msg('编辑成功')
                console.log(res);
                setTimeout(() => {
                    location.href = 'article_list.html'
                }, 1000)
            }
        })
    }

})